const testMsg = {
	utctime: '114305.000',
	weidu: '3037.64348',
	weiduP: 'N',
	jingdu: '10402.17403',
	jingduP: 'E',
	altitude: '470.6',
	height: '0.0',
	speed: '',
	normalSpeed: '3333.738254823',
	course: '',
	cihangxiang: '',
	date: '',
	month: '',
	year: '',
	valid: true,
}
!'114305A3037B6434ANA10402B1740AEA470B6A0B0A3333B7382A0A'

// 辅助函数如果为空
function getStrBlank(fieldStr) {
	let res = ''
	if (fieldStr) {
		let str = fieldStr.split('.')
		if (str[1]) {
			// 判断小数点后面位数大于4,则只取前4位
			if (str[1].length > 4) {
				res = str[0] + 'B' + str[1].substring(0, 4) + 'A'
			} else {
				res = str[0] + 'B' + str[1] + 'A'
			}
		} else {
			res = str[0] + 'A'
		}
	} else {
		res += 0 + 'A'
	}
	return res
}

function sendPositionMsg(msgObj) {
	resStr = ''
	// 1.utctime取秒之前
	resStr += msgObj.utctime.split('.')[0] + 'A'
	// 2.纬度
	resStr += getStrBlank(msgObj.weidu)
	// 3.纬度位置
	resStr += msgObj.weiduP + 'A'
	// 4.经度
	resStr += getStrBlank(msgObj.jingdu)
	// 5.经度位置
	resStr += msgObj.jingduP + 'A'
	// 6.海拔
	resStr += getStrBlank(msgObj.altitude)
	// 7.参考椭球面与大地水准面之间的距离
	resStr += getStrBlank(msgObj.height)
	// 8.速度-千米/小时
	resStr += getStrBlank(msgObj.normalSpeed)
	// 9.真北航向
	resStr += getStrBlank(msgObj.course)
	// 年月日
	resStr += msgObj.year + msgObj.month + msgObj.date + 'A'
	// 有效性
	resStr += msgObj.valid ? '1' : '0'
	return resStr
}
console.log(sendPositionMsg(testMsg))
console.log(sendPositionMsg(testMsg).length)
module.exports = sendPositionMsg
